//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YoYInflationTermStructure.h"
using namespace Cephei::QL::Termstructures;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Inflation/Seasonality.h>
#include <gen/QL/Termstructures/InflationTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Inflation;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (boost::shared_ptr<QuantLib::YoYInflationTermStructure>& childNative, Object^ owner) : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phYoYInflationTermStructure = NULL;
#endif
	_ppYoYInflationTermStructure = &childNative;
    _ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (boost::dynamic_pointer_cast<QuantLib::InflationTermStructure> (*_ppYoYInflationTermStructure));
}
Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (QuantLib::YoYInflationTermStructure& childNative, Object^ owner) : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phYoYInflationTermStructure = NULL;
#endif
	_ppYoYInflationTermStructure = new boost::shared_ptr<QuantLib::YoYInflationTermStructure> (&childNative);
    _ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (boost::dynamic_pointer_cast<QuantLib::InflationTermStructure> (*_ppYoYInflationTermStructure));
    _YoYInflationTermStructureOwner = owner;
    _InflationTermStructureOwner = owner;
}

Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (CYoYInflationTermStructure^ copy) : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phYoYInflationTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationTermStructure = new boost::shared_ptr<QuantLib::YoYInflationTermStructure> (copy->GetShared());
        _ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (boost::dynamic_pointer_cast<QuantLib::InflationTermStructure> (*_ppYoYInflationTermStructure));
    }
}
Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (PLATFORM::Type^ t) : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phYoYInflationTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationTermStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (QuantLib::Handle<QuantLib::YoYInflationTermStructure>& childNative, Object^ owner)  : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
	_phYoYInflationTermStructure = &childNative;
	_ppYoYInflationTermStructure = &static_cast<boost::shared_ptr<QuantLib::YoYInflationTermStructure>>(childNative.currentLink());
    _ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (boost::dynamic_pointer_cast<QuantLib::InflationTermStructure> (*_ppYoYInflationTermStructure));
    _YoYInflationTermStructureOwner = owner;
}
Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (QuantLib::Handle<QuantLib::YoYInflationTermStructure> childNative)  : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
	_phYoYInflationTermStructure = &childNative;
	_ppYoYInflationTermStructure = &static_cast<boost::shared_ptr<QuantLib::YoYInflationTermStructure>>(childNative.currentLink());
    _ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (boost::dynamic_pointer_cast<QuantLib::InflationTermStructure> (*_ppYoYInflationTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::CYoYInflationTermStructure::CYoYInflationTermStructure (QuantLib::YoYInflationTermStructure childNative)  : CInflationTermStructure(CYoYInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phYoYInflationTermStructure = NULL;
#endif
	_ppYoYInflationTermStructure = new boost::shared_ptr<QuantLib::YoYInflationTermStructure> (new QuantLib::YoYInflationTermStructure (childNative));
    _ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (boost::dynamic_pointer_cast<QuantLib::InflationTermStructure> (*_ppYoYInflationTermStructure));
}
#endif

Cephei::QL::Termstructures::CYoYInflationTermStructure::~CYoYInflationTermStructure ()
{
    if (_ppYoYInflationTermStructure != NULL)
    {
	    delete _ppYoYInflationTermStructure;
        _ppYoYInflationTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::CYoYInflationTermStructure::!CYoYInflationTermStructure ()
{
    if (_ppYoYInflationTermStructure != NULL)
    {
	    delete _ppYoYInflationTermStructure;
    }
}
QuantLib::YoYInflationTermStructure& Cephei::QL::Termstructures::CYoYInflationTermStructure::GetReference ()
{
    if (_ppYoYInflationTermStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppYoYInflationTermStructure;
}
boost::shared_ptr<QuantLib::YoYInflationTermStructure>& Cephei::QL::Termstructures::CYoYInflationTermStructure::GetShared ()
{
    if (_ppYoYInflationTermStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppYoYInflationTermStructure;
}
QuantLib::YoYInflationTermStructure* Cephei::QL::Termstructures::CYoYInflationTermStructure::GetPointer ()
{
    if (_ppYoYInflationTermStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppYoYInflationTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationTermStructure>& Cephei::QL::Termstructures::CYoYInflationTermStructure::GetHandle ()
{
	if (_phYoYInflationTermStructure == NULL)
	{
		_phYoYInflationTermStructure = new Handle<QuantLib::YoYInflationTermStructure> (*_ppYoYInflationTermStructure);
	}
	return *_phYoYInflationTermStructure;
}
#endif
bool Cephei::QL::Termstructures::CYoYInflationTermStructure::HasNative () 
{
	return (_ppYoYInflationTermStructure != NULL);
}

Double Cephei::QL::Termstructures::CYoYInflationTermStructure::YoyRate (DateTime d, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ instObsLag, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forceLinearInterpolation, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CinstObsLag;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (instObsLag))
        {
            _CinstObsLag = safe_cast<CPeriod^> (instObsLag->Value);
            _CinstObsLag->Lock();
        }
        QuantLib::Period& _instObsLag = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (instObsLag) ? static_cast<QuantLib::Period&> (_CinstObsLag->GetReference ()) : QuantLib::Period(-1,Days)); //6
        bool _forceLinearInterpolation = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forceLinearInterpolation) ? (bool)ValueHelper::Convert (forceLinearInterpolation->Value) : false); //9a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTermStructure)->yoyRate ( _d,  _instObsLag,  _forceLinearInterpolation,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CinstObsLag != nullptr) _CinstObsLag->Unlock();
    }
}
Double Cephei::QL::Termstructures::CYoYInflationTermStructure::YoyRate (Double t, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationTermStructure)->yoyRate ( _t,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

